<template>
  <div class="plan-detail-table-container">
    <!-- 表格工具栏 -->
    <div class="table-toolbar">
      <div class="toolbar-left">
        <el-input
          v-model="searchText"
          placeholder="搜索订单号、产品编号..."
          size="small"
          style="width: 250px;"
          clearable
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </div>
      <div class="toolbar-right">
        <el-button size="small" @click="exportToExcel">
          <el-icon><Download /></el-icon>
          导出Excel
        </el-button>
        <el-button size="small" @click="refreshTable">
          <el-icon><Refresh /></el-icon>
          刷新
        </el-button>
      </div>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="paginatedData"
      stripe
      border
      height="650"
      :header-cell-style="{ 
        background: '#f5f7fa', 
        color: 'white',
        fontWeight: '600',
        fontSize: '14px'
      }"
      @sort-change="handleSortChange"
    >
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column
        prop="operationName"
        label="工序名称"
        align="center"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="operationCode"
        label="工序编码"
        align="center"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="processStartTime"
        label="计划开始时间"
        min-width="230"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="processEndTime"
        label="计划结束时间"
        min-width="230"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="equiptName"
        label="使用设备"
        min-width="100"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="equiptCode"
        label="使用设备编码"
        align="center"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="operationCode"
        label="工序编码"
        align="center"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="orderCode"
        label="订单编号"
        min-width="230"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="apsOrderCode"
        label="系统内部编号"
        min-width="270"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="orderCount"
        label="订单数量"
        min-width="100"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="productCode"
        label="产品编号"
        align="center"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="productName"
        label="产品名称"
        align="center"
        width="180"
        show-overflow-tooltip
      />
      
      <el-table-column
        prop="orderCode"
        label="订单编号"
        width="140"
        sortable
        show-overflow-tooltip
      />
      
      <el-table-column
        prop="productCode"
        label="产品编号"
        width="140"
        sortable
        show-overflow-tooltip
      />
      
      <el-table-column
        prop="quantity"
        label="订单数量"
        width="100"
        sortable
        align="right"
      >
        <template #default="{ row }">
          <span class="quantity-text">{{ row.quantity }}</span>
        </template>
      </el-table-column>
      
      <el-table-column
        prop="priority"
        label="订单优先级"
        width="100"
        align="center"
        sortable
      >
        <template #default="{ row }">
          <el-tag 
            :type="getPriorityType(row.priority)" 
            size="small"
          >
            {{ row.priority }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column
        prop="processCode"
        label="工序编号"
        width="120"
        show-overflow-tooltip
      />
      
      <el-table-column
        prop="processName"
        label="工序名称"
        width="140"
        show-overflow-tooltip
      />
      
      <el-table-column
        prop="standardTime"
        label="标准工时(h)"
        width="110"
        sortable
        align="right"
      >
        <template #default="{ row }">
          <span class="time-text">{{ row.standardTime }}</span>
        </template>
      </el-table-column>
      
      <el-table-column
        prop="startTime"
        label="工序开始时间"
        width="160"
        sortable
        show-overflow-tooltip
      />
      
      <el-table-column
        prop="endTime"
        label="工序结束时间"
        width="160"
        sortable
        show-overflow-tooltip
      />
      
      <el-table-column
        label="操作"
        width="120"
        fixed="right"
        align="center"
      >
        <template #default="{ row }">
          <el-button 
            type="text" 
            size="small"
            class="custom-link-button"
            @click="viewDetail(row)"
          >
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="计划详情"
      width="600px"
      :close-on-click-modal="false"
    >
      <el-descriptions :column="2" border v-if="selectedRow">
        <el-descriptions-item label="设备编号">{{ selectedRow.deviceCode }}</el-descriptions-item>
        <el-descriptions-item label="订单编号">{{ selectedRow.orderCode }}</el-descriptions-item>
        <el-descriptions-item label="产品编号">{{ selectedRow.productCode }}</el-descriptions-item>
        <el-descriptions-item label="订单数量">{{ selectedRow.quantity }}</el-descriptions-item>
        <el-descriptions-item label="订单优先级">
          <el-tag :type="getPriorityType(selectedRow.priority)">{{ selectedRow.priority }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="工序编号">{{ selectedRow.processCode }}</el-descriptions-item>
        <el-descriptions-item label="工序名称">{{ selectedRow.processName }}</el-descriptions-item>
        <el-descriptions-item label="标准工时">{{ selectedRow.standardTime }}h</el-descriptions-item>
        <el-descriptions-item label="开始时间" :span="2">{{ selectedRow.startTime }}</el-descriptions-item>
        <el-descriptions-item label="结束时间" :span="2">{{ selectedRow.endTime }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Download, Refresh } from '@element-plus/icons-vue'

export default {
  name: 'PlanDetailTable',
  components: {
    Search,
    Download,
    Refresh
  },
  props: {
    // 表格数据
    tableData: {
      type: Array,
      required: true,
      default: () => []
    },
    // 约束 ID
    constraintId: {
      type: String,
      default: ''
    }
  },
  emits: ['refresh'],
  setup(props, { emit }) {
    // 搜索文本
    const searchText = ref('')
    
    // 分页
    const currentPage = ref(1)
    const pageSize = ref(20)
    
    // 排序
    const sortProp = ref('')
    const sortOrder = ref('')
    
    // 详情对话框
    const detailDialogVisible = ref(false)
    const selectedRow = ref(null)

    /**
     * 初始化打印父组件传过来的参数
     */
    console.log('📋 PlanDetailTable 接收到的参数:')
    console.log('- tableData:', props.tableData)
    console.log('- constraintId:', props.constraintId)

    /**
     * 表格数据（直接使用props）
     */
    const tableData = computed(() => props.tableData || [])

    /**
     * 总数
     */
    const totalCount = computed(() => tableData.value.length)

    /**
     * 当前页数据
     */
    const paginatedData = computed(() => {
      const start = (currentPage.value - 1) * pageSize.value
      const end = start + pageSize.value
      return tableData.value.slice(start, end)
    })

    /**
     * 获取优先级标签类型
     */
    const getPriorityType = (priority) => {
      const typeMap = {
        '高': 'danger',
        '中': 'warning',
        '低': 'info'
      }
      return typeMap[priority] || 'info'
    }

    /**
     * 处理排序变化
     */
    const handleSortChange = ({ prop, order }) => {
      sortProp.value = prop
      sortOrder.value = order
    }

    /**
     * 处理分页大小变化
     */
    const handleSizeChange = (val) => {
      pageSize.value = val
      currentPage.value = 1
    }

    /**
     * 处理当前页变化
     */
    const handleCurrentChange = (val) => {
      currentPage.value = val
    }

    /**
     * 查看详情
     */
    const viewDetail = (row) => {
      selectedRow.value = row
      detailDialogVisible.value = true
    }

    /**
     * 刷新表格
     */
    const refreshTable = () => {
      emit('refresh')
      ElMessage.success('表格已刷新')
    }

    /**
     * 导出到 Excel
     */
    const exportToExcel = () => {
      // TODO: 实现 Excel 导出功能
      ElMessage.info('Excel 导出功能开发中...')
    }

    return {
      searchText,
      currentPage,
      pageSize,
      totalCount,
      paginatedData,
      detailDialogVisible,
      selectedRow,
      getPriorityType,
      handleSortChange,
      handleSizeChange,
      handleCurrentChange,
      viewDetail,
      refreshTable,
      exportToExcel
    }
  }
}
</script>

<style scoped>
.plan-detail-table-container {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.table-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  margin-bottom: 10px;
}

.toolbar-left,
.toolbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.quantity-text,
.time-text {
  font-weight: 500;
  color: #303133;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  padding: 15px 0;
}

/* 自定义链接按钮 */
.custom-link-button {
  color: rgb(1, 128, 165);
  font-weight: 600;
  text-decoration: underline;
}

.custom-link-button:hover {
  color: #409EFF;
}
</style>

